<template>
  <div>
    <Modal :value="showModal" :title="title" @on-visible-change="changeVisible">
      <Form ref="form" :model="formData" :rules="ruleCustom">
        <FormItem style="display: none" v-if="formData.id">
          <label>
            <Input v-model="formData.id" :value="formData.id"/>
          </label>
        </FormItem>
        <FormItem label="父级" prop="parent_id">
          <label>
            <Select clearable transfer filterable clearable v-model="formData.parent_id">
              <Option v-for="item in permission" :value="item.id" :key="item.id">{{ item.display_name }}</Option>
            </Select>
          </label>
        </FormItem>
        <FormItem label="权限名称" prop="display_name">
          <label>
            <label>
              <Input v-model="formData.display_name" clearable placeholder="请填写权限名称"/>
            </label>
          </label>
        </FormItem>
        <FormItem label="唯一键" prop="name">
          <label>
            <Input v-model="formData.name" clearable placeholder="请填写权限识别码"/>
          </label>
        </FormItem>
        <FormItem label="排序">
          <label>
            <Input v-model="formData.sort" clearable placeholder="请填写序号"/>
          </label>
        </FormItem>
        <FormItem label="类型">
          <label>
            <RadioGroup v-model="type">
              <Radio label="菜单"/>
              <Radio label="按钮"/>
            </RadioGroup>
          </label>
        </FormItem>
        <Button
          long
          type="primary"
          size="large"
          @click="handleSubmit('form')">确认
        </Button>
      </Form>
      <div slot="footer"/>
    </Modal>
  </div>
</template>

<script>

  export default {
    name: 'permission-handle',
    props: {
      showModal: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: '编辑权限'
      },
      permission: {
        type: Array,
        default() {
          return []
        }
      },
      formData: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    mounted() {
    },
    data() {
      return {
        loading: true,
        type: '菜单',
        ruleCustom: {
          display_name: [
            {required: true, message: '请填写权限名称', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请填写权限唯一键', trigger: 'blur'}
          ],
          parent_id: [
            {required: true, message: '请选择权限父级', trigger: 'blur', type: 'number'},
          ],
        }
      }
    },
    methods: {
      handleSubmit(name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            this.formData.type = this.type === '菜单' ? 1 : 2
            this.$emit('handleSubmit', this.formData)
          } else {
            return false
          }
        })
      },
      changeVisible: function (val) {
        this.$refs['form'].resetFields()
        this.$emit('changeVisible', val)
      },
    }
  }
</script>

<style scoped>

</style>
